package com.yuejian.neusoft.yuejian_24.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuejian.neusoft.yuejian_24.R;
import com.yuejian.neusoft.yuejian_24.javaBean.Train;
import com.yuejian.neusoft.yuejian_24.javaBean.User;
import com.yuejian.neusoft.yuejian_24.listener.TrainListener;
import com.yuejian.neusoft.yuejian_24.listener.UserListener;
import com.yuejian.neusoft.yuejian_24.model.TrainModel;
import com.yuejian.neusoft.yuejian_24.model.UserModel;

public class TrainJoinActivity extends AppCompatActivity {
    private TextView train_name, train_price, username, mustknow, count;
    private ImageView jia, jian, back;
    private EditText userphone, num;
    private Intent intent;
    private SharedPreferences sp;

    TrainListener trainListener = new TrainListener() {
        @Override
        public void onResponse(Train info) {
            train_name.setText(info.getTrain_name());
            train_price.setText(info.getPrice() + "￥/人");
            mustknow.setText("\u3000\u3000" + info.getIntroduction());
            double abc = Double.parseDouble(info.getPrice().toString());
            count.setText("合计：" + String.valueOf(abc) + "￥");
        }

        @Override
        public void onFail() {

        }
    };

    UserListener userListener = new UserListener() {
        @Override
        public void onResponse(User userinfo) {
            username.setText(userinfo.getUsername());
            userphone.setText(userinfo.getUser_phone());
        }

        @Override
        public void onFail() {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_train_join);
        initViews();
        initEvent();
        TrainModel trainModel = new TrainModel();
        trainModel.gettrain(getIntent().getStringExtra("trainid"), trainListener);
        UserModel userModel = new UserModel();
        userModel.getuserinfo(sp.getString("uid", ""), userListener);
        count();
    }

    private void count() {
        num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                double zhongnum = Double.parseDouble(train_price.getText().toString().split("￥")[0]) *
                        Double.parseDouble(num.getText().toString());
                count.setText("合计：" + String.valueOf(zhongnum) + "￥");
            }
        });
    }

    private void initEvent() {
        jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int number = Integer.parseInt(num.getText().toString()) + 1;
                num.setText(String.valueOf(number));
            }
        });

        jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int number = Integer.parseInt(num.getText().toString()) - 1;
                num.setText(String.valueOf(number));
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initViews() {
        train_name = (TextView) findViewById(R.id.train_join_name);
        train_price = (TextView) findViewById(R.id.train_join__price);
        username = (TextView) findViewById(R.id.train_join__username);
        mustknow = (TextView) findViewById(R.id.train_join_know);
        count = (TextView) findViewById(R.id.train_join_count);
        jia = (ImageView) findViewById(R.id.train_join__jia);
        jian = (ImageView) findViewById(R.id.train_join__jian);
        back = (ImageView) findViewById(R.id.train_join_back);
        userphone = (EditText) findViewById(R.id.train_join__phone);
        num = (EditText) findViewById(R.id.train_join__num);
        sp = getSharedPreferences("userinfo", Context.MODE_PRIVATE);
    }
}